﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using Telerik.Web.UI;

namespace EHTWebsiteProject.Administrator
{
    public partial class AdminCustomer : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));    

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                LoadDataUsers();
            }
        }


        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbUserManager.Text = rm.GetString("lbUserManager", ci);           
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbLock.Text = rm.GetString("lbLock", ci);
            lbUnLock.Text = rm.GetString("lbUnLock", ci);          
        }

        private void LoadDataUsers()
        {
            string[] usersInRole = Roles.GetUsersInRole(HangSo.ROLE_CUS);
            MembershipUserCollection users = new MembershipUserCollection();


            try
            {
                foreach (string userName in usersInRole)
                {
                    users.Add(Membership.GetUser(userName));
                }
            }
            catch { }


            this.rpUser.DataSource = users;
            this.rpUser.DataBind();

            //thiết định Role cho User
            foreach (RepeaterItem item in rpUser.Items)
            {
                Literal ltrUserName = (Literal)item.FindControl("ltrUserName");
                Literal ltrRole = (Literal)item.FindControl("ltrRole");
                //ltrRole.Text = Roles.GetRolesForUser(ltrUserName.Text.Trim())[0];
            }


        }

        protected void ImgUnLock_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            foreach (RepeaterItem rp in rpUser.Items)
            {
                CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                Label lbDelID = (Label)rp.FindControl("lbDelID");
                if (ckMenu.Checked == true)
                {
                    arr.Add(lbDelID.Text);
                }
            }

            for (int i = 0; i < arr.Count; i++)
            {
                try
                {
                    MembershipUser theUser = Membership.GetUser(arr[i].ToString());
                    theUser.IsApproved = true;
                    Membership.UpdateUser(theUser);
                    theUser.UnlockUser();
                }
                catch
                {

                }
            }
            LoadDataUsers();
        }

        protected void ImgLock_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();

            foreach (RepeaterItem rp in rpUser.Items)
            {
                CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                Label lbDelID = (Label)rp.FindControl("lbDelID");
                if (ckMenu.Checked == true)
                {
                    arr.Add(lbDelID.Text);
                }
            }
           

            for (int i = 0; i < arr.Count; i++)
            {
                try
                {
                    MembershipUser theUser = Membership.GetUser(arr[i].ToString());
                    theUser.IsApproved = false;
                    Membership.UpdateUser(theUser);
                }
                catch
                {

                }
            }
            LoadDataUsers();
        }


        public void CheckAll(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
           
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpUser.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpUser.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
           
        }

        protected void rpUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Image ImaStatus = (Image)e.Item.FindControl("ImaStatus");
            Label lbDelID = (Label)e.Item.FindControl("lbDelID");

            try
            {
                MembershipUser theUser = Membership.GetUser(lbDelID.Text);
                if (theUser.IsApproved == false || theUser.IsLockedOut == true)
                    ImaStatus.ImageUrl = "~/images/EHTgiaodien/icon-32-Lock.jpeg";
                else
                    ImaStatus.ImageUrl = "~/images/unlock.jpeg";
            }
            catch
            {

            }
        }
    }
}